package com.wlts.wzy.TestDemo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.QueryBizItemListRequest;
import com.aliyuncs.linkedmall.model.v20180116.QueryBizItemListResponse;
import com.kuaishou.merchant.open.api.KsMerchantApiException;
import com.kuaishou.merchant.open.api.client.AccessTokenKsMerchantClient;
import com.kuaishou.merchant.open.api.client.oauth.OauthCredentialKsClient;
import com.kuaishou.merchant.open.api.request.KsMerchantItemAddRequest;
import com.kuaishou.merchant.open.api.request.param.item.MerchantServiceRuleParam;
import com.kuaishou.merchant.open.api.request.param.item.MerchantSkuInfoParam;
import com.kuaishou.merchant.open.api.response.KsMerchantItemAddResponse;
import com.wlts.wzy.LMGoodsModule.entity.LmGoods;
import com.wlts.wzy.LMGoodsModule.service.ILmGoodsService;
import com.wlts.wzy.configBean.LmConfig.LmConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class PutGoodsToKS {
    @Autowired
    private ILmGoodsService lmGoodsService;
    private static final String appKey = "ks664280946453531393";
    private static String grantCode = "55121cf8a93aa4404335c4d3e9a1773d02ea1445c65b965a724f440d8b856961e12ee58f";

    private  static final String signSecret = "554a351fd517a39d6860d91d29b92a13";
    private static String accessToken = "";
    private static OauthCredentialKsClient oauthCredentialKsClient;

    static {
        String appSecret = "0qP7CL-Hqi-4Ev6RlK9-zA";
        oauthCredentialKsClient
                = new OauthCredentialKsClient(appKey, appSecret);
    }

    /**
     * 增加商品
     * @throws KsMerchantApiException
     */
  //  @PostConstruct
    public void addGoods()  {

        System.out.println("开始传商品到KS");


        List<LmGoods> lmGoodsList=lmGoodsService.list(new MyLambdaQueryWrapper<LmGoods>().eq(LmGoods::getCanSell,0));

        lmGoodsList.forEach(lmGoods -> {
            KsMerchantItemAddRequest goods = new KsMerchantItemAddRequest();
            goods.setAccessToken(accessToken);
            goods.setRelItemId(lmGoods.getItemId());

            String title=lmGoods.getItemTitle();

            if(title.length()>30){
                title=title.substring(0,30);
            }

            goods.setTitle(title);

            List<String> array = new ArrayList<>();
            array.add("https:"+lmGoods.getMainPicPrl());
            goods.setImageUrls(array);
            goods.setDetails(lmGoods.getItemTitle());

            goods.setExpressFee(0);

            MerchantServiceRuleParam ruleParam = new MerchantServiceRuleParam();
            ruleParam.setRefundRule("1");
            //   ruleParam.setTheDayOfDeliverGoodsTime(9);
            ruleParam.setPromiseDeliveryTime(172800);

            ruleParam.setDepositRule("1");

            goods.setServiceRule(ruleParam);

            goods.setCategoryId(1125);
            goods.setCategoryName("生活日用");
            goods.setParentCategoryId(1124);
            goods.setParentCategoryName("居家日用");

            List<MerchantSkuInfoParam> skuList = new ArrayList<>();

            List<QueryBizItemListResponse.Item.Sku> itemList= JSONArray.parseArray(lmGoods.getSkuList(),QueryBizItemListResponse.Item.Sku.class);

            for (QueryBizItemListResponse.Item.Sku item:itemList
                 ) {
                MerchantSkuInfoParam param = new MerchantSkuInfoParam();
                param.setRelSkuId(item.getSkuId());
                param.setImageUrl("https:"+item.getSkuPicUrl());
                param.setSkuSpecification(item.getSkuTitle());
                param.setSkuStock(2L);

                //{"canSell":false,"customizedAttributeMap":{},"gradePriceModels":[],"points":0,"pointsAmount":0,"priceCent":710,"skuId":4549611461422,"skuPicUrl":"//img.alicdn.com/imgextra/i1/2201498506788/O1CN01AFNQHf200x92Dshep_!!2-item_pic.png","skuTitle":"新年送福红袜子/均码","userLabelList":[]}
                param.setSkuSalePrice(item.getPriceCent());
                skuList.add(param);
            }
            goods.setSkuInfoParams(skuList);

            goods.setExpressTemplateId(5995512890L);
            // goods.setCategoryId("");


            AccessTokenKsMerchantClient tokenKsMerchantClient
                    = new AccessTokenKsMerchantClient("", appKey, signSecret);

            KsMerchantItemAddResponse response= null;
            try {
                response = tokenKsMerchantClient.execute(goods);
            } catch (KsMerchantApiException e) {
                e.printStackTrace();
            }

            System.out.println(JSON.toJSONString(response));
        });


    }







}
